<?php
/**
 * PHP version 5
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    User
 * @subpackage Action
 * @author     Aljaz Srebrnic <a2piratesoft@gmail.com>
 * @author     E. Pavletič
 * @copyright  2008 a2PirateSoftware
 * @license    GPL License v3 <http://www.gnu.org/copyleft/gpl.html>
 */

require_once('ZNF/Presentation/Translation.php');
require_once(ZNF::MODULES_DIR . '/User/Business/UserDAO.php');
require_once(ZNF::MODULES_DIR . '/User/Business/RoleDAO.php');
require_once('class.CommonAction.php');

class User_Action_Login extends CommonAction
{

    public function requestLogin($form, ZNF_Action_ActionMapping $mapping)
    {
        $translation = new ZNF_Presentation_Translation('User', $_SESSION['znf']['lang']);
        $errors = new ZNF_Action_ActionErrors();

        if($_SESSION['znf']['authenticated'])
        {
            $errors->addError(1, $translation->get('errorUserAlredyLoggedIn'));
            $_REQUEST['errors'] = $errors;
            return $mapping->findForward('failure');
        }

        //$_REQUEST['key'] = self::sendLoginToken();
        return $mapping->findForward('success');

    }

    public function login(ZNF_Action_ActionForm $form, ZNF_Action_ActionMapping $mapping)
    {
        $username = $form->getUsername();
        $password = $form->getPassword();
		$translation = new ZNF_Presentation_Translation('User', $_SESSION['znf']['lang']);

        if ($_SESSION['znf']['authenticated']) {
            //user already authenticated
            $errors = new ZNF_Action_ActionErrors();
            $errors->addError(1, $translation->get('errorUserAlreadyLoggedIn'));
            $_REQUEST['errors'] = $errors;
            return $mapping->findForward('failure');
        }

        //user not authenticated
        $userDAO = new User_Business_UserDAO();
        $roleDAO = new User_Business_RoleDAO();

        try{
            $user = $userDAO->authUser($username, $password);
        }catch (User_Business_UserDAOException $e){
            $_REQUEST['errors'] = self::handleException($e, 'User');
            return $mapping->findForward('failure');
        }

        if (!isset($user['username']))
        {
            $translation = new ZNF_Presentation_Translation('User', $_SESSION['znf']['lang']);
            $errors = new ZNF_Action_ActionErrors();
            $errors->addError(1, $translation->get('errorIncorrectUserOrPassword'));
            $_REQUEST['errors'] = $errors;
            return $mapping->findForward('failure');
        }

		if (!$user['active'])
        {
            $translation = new ZNF_Presentation_Translation('User', $_SESSION['znf']['lang']);
            $errors = new ZNF_Action_ActionErrors();
            $errors->addError(1, $translation->get('errorUserNotActive'));
            $_REQUEST['errors'] = $errors;
            return $mapping->findForward('failure');
        }

        $_SESSION['id'] = $user['id'];
        $_SESSION['znf']['lang']   = $user['lang'];
        $_SESSION['znf']['locale'] = $user['locale'];
        $_SESSION['znf']['theme']  = $user['theme'];
        try{
            $result = $roleDAO->getUserRoles($user['id']);
        }catch(User_Business_User_Business_RoleDAOException $e){
            $_REQUEST['errors'] = self::handleException($e, 'User');
            return $mapping->findForward('failure');
        }
        $roles = array();
        foreach ($result as $current) {
           array_push($roles, $current['userrolename']);
        }
        $_SESSION['znf']['roles'] = $roles;
        $_SESSION['znf']['authenticated'] = true;

        // regenerate session ID after succesful login
	setcookie ('loginToken', false, time()-(60*60*24));
        session_regenerate_id();
	$_REQUEST['page'] = '<h2>' . $translation->get('loginSuccess') . '</h2>';
        return $mapping->findForward('success');
    }

    public function __destruct()
    {
    }

}

?>